package com.hotel.api.service.impl;

import com.hotel.api.entity.Room;
import com.hotel.api.entity.User;
import com.hotel.api.entity.UserChangeRoom;
import com.hotel.api.mapper.RoomMapper;
import com.hotel.api.mapper.UserChangeroomMapper;
import com.hotel.api.service.RoomService;
import com.hotel.api.vo.UserChangerRoomVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * TODO
 *
 * @Author 涛涛
 * @Create 2019/8/29
 */
@Service
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private UserChangeroomMapper userChangeroomMapper;


    @Override
    public List<Room> selectUserRoomsByRoomNum(Integer rid) {
        return roomMapper.selectUserRoomsByRoomNum(rid);
    }

    @Override
    public int insert(Room record) {
        return roomMapper.insert(record);
    }

    @Override
    public List<Room> selectAll() {
        return roomMapper.selectAll();
    }

    @Override
    public List<Room> selectUserRooms() {
        return roomMapper.selectUserRooms();
    }

    @Override
    public List<Room> selectRoomByTypeRoom(int type) {
        return roomMapper.selectRoomByTypeRoom(type);
    }

    @Override
    public Room selectRoomByRoomNumber(int room_number) {
        return roomMapper.selectRoomByRoomNumber(room_number);
    }

    @Override
    public List<Room> selectNameByRoomNumber(int room_number) {
        return roomMapper.selectNameByRoomNumber(room_number);

    }


    @Override
    public List<Room> selectRoomByCompany(String company) {
        return roomMapper.selectRoomByCompany(company);
    }

    @Override
    public int deleteRoomByRoomNumber(Integer rid) {
        return roomMapper.deleteRoomByRoomNumber(rid);
    }

    @Override
    public int updateRoomByType(Room room) {
        return roomMapper.updateRoomByType(room);
    }

    @Override
    public int updateLivePeopleNumberAddByRoom(int roomNumber) {
        Room room = roomMapper.selectRoomByRoomNumber(roomNumber);
        if (room.getLivedPeopleNumber() < room.getType()) {
            return roomMapper.updateLivePeopleNumberAddByRoom(roomNumber);
        } else {
            return 0;
        }

    }

    @Override
    public int updateLivePeopleNumberReduceByRoom(int roomNumber) {
        Room room = roomMapper.selectRoomByRoomNumber(roomNumber);
        if (room.getLivedPeopleNumber() > 0) {
            return roomMapper.updateLivePeopleNumberReduceByRoom(roomNumber);
        } else {
            return 0;
        }
    }

    @Override
    public int updateRoomByPhoto(String photo, int roomNumber) {
        return roomMapper.updateRoomByPhoto(photo, roomNumber);
    }

    @Override
    public int updateRoomByCompany(String company, int roomNumber) {
        return roomMapper.updateRoomByCompany(company, roomNumber);
    }

    @Override
    public int updateRoomInfoById(Room room) {
        return roomMapper.updateRoomInfoById(room);
    }


    @Override
    public int userChangeRoom(UserChangeRoom userChangeRoom) {
//        userChangeRoom.setCreateTime(new Date());
        return userChangeroomMapper.insert(userChangeRoom);
    }


    public int updateRoomByChange(String name, String idCard, int newRoom) {
        int oldRoom = roomMapper.selectRoomByNameAndIdCard(name, idCard);
        roomMapper.updateLivePeopleNumberReduceByRoom(oldRoom);
        roomMapper.updateLivePeopleNumberAddByRoom(newRoom);
        return 1;

    }

    public List<UserChangeRoom> selectUserChangeRoomOrders() {
        return userChangeroomMapper.selectUserChangeRoomOrders();
    }

    public List<Room> selectEntryRoom() {
        return roomMapper.selectEntryRoom();
    }

    @Override
    public int updateChangeRoomOrderState(UserChangeRoom userChangeRoom) {
        return userChangeroomMapper.update(userChangeRoom);
    }

    @Override
    public UserChangerRoomVO selectUserChangeRoomById(Integer id) {
        return userChangeroomMapper.selectUserChangeRoomById(id);
    }

    @Override
    public int addRoomNumber(Integer roomNum) {
        return roomMapper.addRoomNumber(roomNum);
    }

    @Override
    public int reduceRoomNumber(Integer roomNum) {
        return roomMapper.reduceRoomNumber(roomNum);
    }

    @Override
    public UserChangeRoom selectUserChangeRoomByUserId(Integer id) {
        return userChangeroomMapper.selectUserChangeRoomByUserId(id);
    }

}
